﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;

namespace ProjectQuaDep.Control
{
    public partial class Pagging : System.Web.UI.UserControl
    {
        private int _index = 0;
        private string _cate = string.Empty;
        private string _extention;

        public int Index
        {
            get { return _index; }
            set { _index = value; }
        }
        public string Cate
        {
            get { return _cate; }
            set { _cate = value; }
        }

        public string Extention
        {
            get { return _extention != "" && _extention != null ? "." + _extention.TrimStart('.') : ".htm"; }
            set { _extention = value; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            {
                this.PreRender += new EventHandler(Pagging_PreRender);
            }
        }

        void Pagging_PreRender(object sender, EventArgs e)
        {
            try
            {
                StringBuilder b = new StringBuilder("");
                if (Index <= 1)
                {
                    Index = 1;
                }
                else
                {
                    b.Append("<a href='" + Cate + "trang-" + Convert.ToString(Index - 1) + Extention + "' ><span class=\"prev\"></span></a>&nbsp;");
                }
                if (Index <= 7)
                {
                    for (int i = 1; i < 10; i++)
                    {
                        if (i == Index)
                        {
                            b.Append("<a class=\"currentpage\">" + i.ToString() + "</a>&nbsp;");
                        }
                        else
                        {
                            b.Append("<a href='" + Cate + "trang-" + i.ToString() + Extention + "' >" + i.ToString() + "</a>&nbsp;");
                        }
                    }
                }
                else
                {
                    b.Append("<a href='" + Cate + "trang-1.htm'>1</a>&nbsp;");
                    b.Append("<a href='" + Cate + "trang-2.htm'>2</a>&nbsp;");
                    b.Append("<a href='" + Cate + "trang-3.htm'>3</a>&nbsp;");
                    b.Append("<a class=\"\">...</a>&nbsp;");

                    for (int i = (Index - 3); i <= (Index + 3); i++)
                    {
                        if (i == Index)
                        {
                            b.Append("<a  class=\"currentpage\">" + i.ToString() + "</a>&nbsp;");

                        }
                        else
                        {
                            b.Append("<a href='" + Cate + "trang-" + i.ToString() + Extention + "' >" + i.ToString() + "</a>&nbsp;");
                        }
                    }
                }

                if (Index >= 1)
                {
                    b.Append("<a href='" + Cate + "trang-" + (Index + 1) + Extention + "' ><span class=\"next\" ></span></a>&nbsp;");
                }
                else
                {
                    b.Append("<a href='" + Cate + "trang-" + Index + Extention + "' ><span class=\"next\" ></span></a>&nbsp;");
                }

                this.ltrPaging.Text = b.ToString();
            }
            catch
            { }
        }
    }
}